package com.jrh.web.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jrh.biz.brand.BrandService;
import com.jrh.biz.category.CategoryService;
import com.jrh.biz.feature.FeatureService;
import com.jrh.biz.hall.HallService;
import com.jrh.biz.product.ProductService;
import com.jrh.biz.product.ref.ProductRefService;
import com.jrh.biz.product.set.ProductSetService;
import com.jrh.dal.CategoryDao;
import com.jrh.dal.LabelDao;
import com.jrh.domain.entity.Brand;
import com.jrh.domain.entity.Category;
import com.jrh.domain.entity.Feature;
import com.jrh.domain.entity.Hall;
import com.jrh.domain.entity.Label;
import com.jrh.domain.entity.Product;
import com.jrh.domain.entity.ProductRef;
import com.jrh.domain.entity.ProductSet;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService    productService;
    @Autowired
    private ProductRefService productRefService;
    @Autowired
    private BrandService      brandService;
    @Autowired
    private LabelDao          labelDao;
    @Autowired
    private CategoryService   categoryService;
    @Autowired
    private ProductSetService productSetService;
    @Autowired
    private HallService       hallService;
    @Autowired
    private CategoryDao       categoryDao;
    @Autowired
    private FeatureService    featureService;

    @RequestMapping("/p{id}")
    public String index(@PathVariable Long id, Model model) {
        Product product = productService.getAvailableProductById(id);
        if (product == null) {
            return "notFound";
        }
        Brand brand = brandService.getBrandById(product.getBrandId());
        ProductRef productAd = productRefService.getProductAd(id);
        List<ProductRef> productRefGroup = productRefService.listSameGroupProduct(id);
        model.addAttribute("product", product);
        model.addAttribute("brand", brand);
        model.addAttribute("productAd", productAd);
        model.addAttribute("productRefGroup", productRefGroup);

        // 系列信息
        List<ProductSet> setList = productSetService.listSetsByBrandId(brand.getId());
        model.addAttribute("setList", setList);

        // 展厅信息
        List<Hall> hallList = hallService.listHallByBrandId(brand.getId());
        model.addAttribute("hallList", hallList);

        // 类目信息
        List<Category> catList = categoryDao.listAllCategoryByBrand(brand.getId());
        Map<String, List<Category>> catmap = new HashMap<String, List<Category>>();
        List<Category> list = null;
        for (Category cat : catList) {
            if (cat == null || cat.getIsParent() == null) {
                continue;
            }
            if (cat.getIsParent() == 1) {
                list = new ArrayList<Category>();
                catmap.put(cat.getName(), list);
                continue;
            } else {
                list.add(cat);
            }
        }
        model.addAttribute("catmap", catmap);
        return "product/index";
    }

    @RequestMapping("/select")
    public String select(ProductCondition productCondition, Model model) {
        List<Label> labels = labelDao.listChildLabel(Label.LABEL_STYLE_KEY);
        List<Category> categoryList = categoryService.listCategoryByParent();
        int space = 7 - 2 * (categoryList.size() % 3);
        
        if (StringUtils.isNotEmpty(productCondition.getCategory())) {
            List<Feature> featureList = featureService.listFeatureDetailByCategory(productCondition.getCategory());
            model.addAttribute("showMore", true);
            model.addAttribute("featureList", featureList);
        }
        model.addAttribute("labels", labels);
        model.addAttribute("categorys", categoryList);
        model.addAttribute("space", space);
        model.addAttribute("productCondition", productCondition);
        return "product/select";
    }
}
